package com.jd.tank;

import com.jd.tank.abstractfactory.*;
import com.jd.tank.abstractfactory.rectangle.RectangleFactory;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @program: MyTank
 * @description:
 * @author: 王术平
 * @create: 2020-11-25 23:23
 **/
public class TankFrame extends Frame {
    public static final int GAME_WIDTH = 1080, GAME_HEIGHT = 960;
    public List<BaseTank> tanks = new ArrayList<BaseTank>();
    public List<BaseBullet> bullets = new ArrayList<BaseBullet>();
    public List<BaseExplode> explodes = new ArrayList<BaseExplode>();
    private BaseTank myTank = new Tank(300, 300, Direction.DOWN, Group.GOOD, this, false);


    //    public BaseGameFactory bf = DefaultFactory.getDefaultFactory();
    public BaseGameFactory bf = RectangleFactory.getRectangleFactory();


    private int x = 200;
    private int y = 200;

    public TankFrame() throws HeadlessException {
        this.setSize(GAME_WIDTH, GAME_HEIGHT);
        this.setResizable(true);
        this.setTitle("坦克王");
        this.setBackground(Color.BLACK);
        this.setVisible(true);
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        this.addKeyListener(new MyKeyAdapter());
    }

    Image offScreenImage = null;

    @Override
    public void update(Graphics g) {
        if (offScreenImage == null) {
            offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGHT);
        }
        Graphics gOffScreen = offScreenImage.getGraphics();
        Color c = gOffScreen.getColor();
        gOffScreen.setColor(Color.BLACK);
        gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
        gOffScreen.setColor(c);
        paint(gOffScreen);
        g.drawImage(offScreenImage, 0, 0, null);
    }

    @Override
    public void paint(Graphics g) {
        g.fillRect(x, y, 50, 50);
        super.paint(g);
        Color c = g.getColor();
        g.setColor(Color.WHITE);
        g.drawString("bullets:" + bullets.size(), 10, 60);
        g.drawString("tanks:" + tanks.size(), 10, 80);
        g.drawString("explodes" + explodes.size(), 10, 100);
        g.setColor(c);

        myTank.paint(g);
        for (int i = 0; i < bullets.size(); i++) {
            bullets.get(i).paint(g);
        }

        //java8 stream api
        tanks.stream().forEach((e) -> e.paint(g));
        for (Iterator<BaseTank> it = tanks.iterator(); it.hasNext(); ) {
            BaseTank tank = it.next();
            tank.paint(g);
            if (!tank.isLiving()) it.remove();
        }

        for (Iterator<BaseExplode> it = explodes.iterator(); it.hasNext(); ) {
            BaseExplode explode = it.next();
            explode.paint(g);
            if (!explode.isLiving()) it.remove();
        }
        for (Iterator<BaseBullet> it = bullets.iterator(); it.hasNext(); ) {
            BaseBullet bullet = it.next();
            for (BaseTank tank : tanks) {
                bullet.collideWith(tank);
            }
        }

    }


    class MyKeyAdapter extends KeyAdapter {

        boolean bL = false;
        boolean bU = false;
        boolean bR = false;
        boolean bD = false;

        @Override
        public void keyPressed(KeyEvent e) {
            int key = e.getKeyCode();

            switch (key) {
                case KeyEvent.VK_LEFT:
                    bL = true;
                    setMainTankDir();
                    break;
                case KeyEvent.VK_UP:
                    bU = true;
                    setMainTankDir();
                    break;
                case KeyEvent.VK_RIGHT:
                    bR = true;
                    setMainTankDir();
                    break;
                case KeyEvent.VK_DOWN:
                    bD = true;
                    setMainTankDir();
                    break;
                case KeyEvent.VK_Q:
                    System.out.println("000");
                    bf=DefaultFactory.getDefaultFactory();
                    break;
                case KeyEvent.VK_W:
                    bf=RectangleFactory.getRectangleFactory();
                    break;
                default:
                    break;
            }

            new Thread(() -> new Audio("audio/tank_move.wav").play()).start();
        }

        @Override
        public void keyReleased(KeyEvent e) {
            int key = e.getKeyCode();

            switch (key) {
                case KeyEvent.VK_LEFT:
                    bL = false;
                    setMainTankDir();
                    break;
                case KeyEvent.VK_UP:
                    bU = false;
                    setMainTankDir();
                    break;
                case KeyEvent.VK_RIGHT:
                    bR = false;
                    setMainTankDir();
                    break;
                case KeyEvent.VK_DOWN:
                    bD = false;
                    setMainTankDir();
                    break;
                case KeyEvent.VK_SPACE:
                    myTank.fire();
                    break;

                default:
                    break;
            }


        }

        private void setMainTankDir() {
            //save the old dir
            Direction dir = myTank.getDir();

            if (!bL && !bU && !bR && !bD) {
                myTank.setMoving(false);
            } else {
                if (bL)
                    myTank.setDir(Direction.LEFT);
                if (bU)
                    myTank.setDir(Direction.UP);
                if (bR)
                    myTank.setDir(Direction.RIGHT);
                if (bD)
                    myTank.setDir(Direction.DOWN);
                //发出坦克移动的消息
                myTank.setMoving(true);
            }
        }
    }
}
